﻿using Autofac;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Domain;
using Volo.Abp.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Volo.Abp.SqlSugarCore.Repositories.DependencyInjection
{
    public static class RepositoryRegister
    {
        public static void AddDefaultRepository(IServiceCollection builder, Assembly assem)
        {
            RegisterDefaultRepositories(builder, assem);
        }
        public static IEnumerable<Type> GetEntityTypes(this Assembly assem)
        {
            return assem.GetTypes().Where(e => e.IsAbstract == false && e.IsClass == true && typeof(IEntity).IsAssignableFrom(e));
        }
        public static Type GetRepositoryType(Type entityType)
        {
            return typeof(SqlSugarCoreRepository<>).MakeGenericType(entityType);
        }

        public static Type GetRepositoryType(Type entityType, Type primaryKeyType)
        {
            return typeof(SqlSugarCoreRepository<,>).MakeGenericType(entityType, primaryKeyType);
        }

        public static void RegisterDefaultRepositories(IServiceCollection builder, Assembly assem)
        {
            foreach (var entityType in assem.GetEntityTypes())
            {
                if (!ShouldRegisterDefaultRepositoryFor(entityType))
                {
                    continue;
                }

                RegisterDefaultRepository(builder, entityType);
            }
        }


        public static void RegisterDefaultRepository(IServiceCollection builder, Type entityType)
        {
            builder.AddDefaultRepository(
                entityType,
                GetDefaultRepositoryImplementationType(entityType)
            );
        }

        public static Type GetDefaultRepositoryImplementationType(Type entityType)
        {
            var primaryKeyType = EntityHelper.FindPrimaryKeyType(entityType);

            if (primaryKeyType == null)
            {
                return GetRepositoryType(entityType);
            }

            return GetRepositoryType(entityType, primaryKeyType);
        }

        public static bool ShouldRegisterDefaultRepositoryFor(Type entityType)
        {

            if (!typeof(IEntity).IsAssignableFrom(entityType))
            {
                return false;
            }

            return true;
        }



        public static void AddSugarRepository(IServiceCollection builder, Assembly assem)
        {
            foreach (var entityType in assem.GetEntityTypes())
            {
                if (!ShouldRegisterDefaultRepositoryFor(entityType))
                {
                    continue;
                }
                var repositoryImplementationType = GetDefaultRepositoryImplementationType(entityType);


                //IRepository<TEntity>
                var repositoryInterface = typeof(ISqlSugarCoreRepository<>).MakeGenericType(entityType);
                if (repositoryInterface.IsAssignableFrom(repositoryImplementationType))
                {

                    //builder.RegisterType(implementationType).As(serviceType).InstancePerDependency().PropertiesAutowired();
                    builder.TryAddTransient(repositoryInterface, repositoryImplementationType);
                }

                //IRepository<TEntity, TKey>
                var primaryKeyType = EntityHelper.FindPrimaryKeyType(entityType);
                var repositoryInterfaceWithPk = typeof(ISqlSugarCoreRepository<,>).MakeGenericType(entityType, primaryKeyType);
                if (repositoryInterfaceWithPk.IsAssignableFrom(repositoryImplementationType))
                {

                    //builder.RegisterType(implementationType).As(serviceType).InstancePerDependency().PropertiesAutowired();
                    builder.TryAddTransient(repositoryInterfaceWithPk, repositoryImplementationType);
                }

            }
        }
    }
}
